import exercise.Exercise;
import exercise.Iterator;

import javax.swing.*;
import java.awt.*;
import java.text.NumberFormat;
import java.util.Random;


public class GUI {

    public GUI() {
    }

    public void showGUI() {
        DatabaseOperator databaseOperator = DatabaseOperator.getInstance();
        Exercise data = databaseOperator.loadExercises(1000);
        Exercise wrongSetOfEquations = new Exercise();
        final Double[] correctRate = {0.0};
        final int[] wrongTime = {0};
        final int[] correctTime = {0};
        Integer[] index = new Integer[5];
        final Integer[] score = {0};

        JFrame jFrame= new JFrame("Practice calculating with integers");
        jFrame.setSize(1200,600);
        JPanel cards = new JPanel(new CardLayout()); // 卡片式布局的面板
        CardLayout cardLayout = (CardLayout)(cards.getLayout()); // 面板切换器
        JPanel mainPanel = new JPanel();   // 主面板
        mainPanel.setLayout(null);
        mainPanel.setSize(1200, 600);
        mainPanel.setBackground(new Color(184,207,229));
        JPanel panelOne = new JPanel();    // 面板1
        panelOne.setLayout(null);
        panelOne.setSize(1200, 600);
        panelOne.setBackground(new Color(184,207,229));
        JPanel panelTwo = new JPanel();    // 面板2
        panelTwo.setLayout(null);
        panelTwo.setSize(1200, 600);
        panelTwo.setBackground(new Color(184,207,229));
        JPanel panelThree = new JPanel();  // 面板3
        panelThree.setLayout(null);
        panelThree.setSize(1200, 600);
        panelThree.setBackground(new Color(184,207,229));

        // 错题簿界面
        JTextField titleField = new JTextField("您目前共有" + wrongSetOfEquations.getData().size() + "道错题");
        titleField.setBackground(new Color(184,207,229));
        titleField.setFont(new Font("楷体",Font.BOLD,32));
        titleField.setBounds(0, 0, 1200, 100);
        titleField.setHorizontalAlignment(JTextField.CENTER);
        titleField.setEditable(false);
        titleField.setBorder(null);
        JTextArea wrongEquationDisplayArea = new JTextArea();
        wrongEquationDisplayArea.setFont(new Font("楷体",Font.BOLD,32));
        wrongEquationDisplayArea.setBounds(30,100,1120,350);
        wrongEquationDisplayArea.setEditable(false);
        wrongEquationDisplayArea.setBorder(null);
        JScrollPane jScrollPaneOfWrongEquationDisplayArea = new JScrollPane(wrongEquationDisplayArea);
        jScrollPaneOfWrongEquationDisplayArea.setBounds(30,100,1120,350);
        jScrollPaneOfWrongEquationDisplayArea.getVerticalScrollBar().setUI(new MyScrollBarUI());
        JButton backToMainButton = new JButton("返回主菜单");
        backToMainButton.setFont(new Font("楷体",Font.BOLD,32));
        backToMainButton.setBounds(500, 470,200, 60);
        backToMainButton.setFocusPainted(false);
        backToMainButton.setBorder(null);
        backToMainButton.setBackground(new Color(255, 255, 255, 255));
        backToMainButton.addActionListener(e -> {
            cardLayout.show(cards, "mainPanel");
            wrongEquationDisplayArea.setText("");
        });
        panelTwo.add(titleField);
        panelTwo.add(jScrollPaneOfWrongEquationDisplayArea);
        panelTwo.add(backToMainButton);

        // 统计界面
        JTextField correctTimeField = new JTextField("答对的题目数: " + correctTime[0]);
        correctTimeField.setBackground(new Color(184,207,229));
        correctTimeField.setFont(new Font("楷体",Font.BOLD,100));
        correctTimeField.setBounds(0, 20, 1200, 100);
        correctTimeField.setHorizontalAlignment(JTextField.CENTER);
        correctTimeField.setEditable(false);
        correctTimeField.setBorder(null);
        panelThree.add(correctTimeField);
        JTextField wrongTimeField = new JTextField("答错的题目数: " + wrongTime[0]);
        wrongTimeField.setBackground(new Color(184,207,229));
        wrongTimeField.setFont(new Font("楷体",Font.BOLD,100));
        wrongTimeField.setBounds(0, 120, 1200, 100);
        wrongTimeField.setHorizontalAlignment(JTextField.CENTER);
        wrongTimeField.setEditable(false);
        wrongTimeField.setBorder(null);
        panelThree.add(wrongTimeField);
        NumberFormat num = NumberFormat.getPercentInstance();
        num.setMaximumFractionDigits(2);
        JTextField correctRateField = new JTextField("正确率: " + num.format(correctRate[0]));
        correctRateField.setBackground(new Color(184,207,229));
        correctRateField.setFont(new Font("楷体",Font.BOLD,100));
        correctRateField.setBounds(0, 220, 1200, 100);
        correctRateField.setHorizontalAlignment(JTextField.CENTER);
        correctRateField.setEditable(false);
        correctRateField.setBorder(null);
        panelThree.add(correctRateField);
        JButton returnButton = new JButton("返回主菜单");
        returnButton.setFont(new Font("楷体",Font.BOLD,64));
        returnButton.setBounds(300, 350,600, 171);
        returnButton.setFocusPainted(false);
        returnButton.setBorder(null);
        returnButton.setBackground(new Color(255, 255, 255, 255));
        returnButton.addActionListener(e -> cardLayout.show(cards, "mainPanel"));
        panelThree.add(returnButton);

        // 练习界面
        JTextField introductionField = new JTextField("熟能生巧 Practice Making Perfect");
        introductionField.setBackground(new Color(184,207,229));
        introductionField.setFont(new Font("楷体",Font.BOLD,32));
        introductionField.setBounds(0, 0, 1200, 100);
        introductionField.setHorizontalAlignment(JTextField.CENTER);
        introductionField.setEditable(false);
        introductionField.setBorder(null);
        panelOne.add(introductionField);
        // 题目域
        JTextField equationFieldOne = new JTextField("");
        equationFieldOne.setEditable(false);
        equationFieldOne.setBounds(50, 100, 300,80);
        equationFieldOne.setFont(new Font("楷体",Font.BOLD,32));
        equationFieldOne.setHorizontalAlignment(JTextField.CENTER);
        equationFieldOne.setBackground(new Color(255, 255, 255, 255));
        JTextField equationFieldTwo = new JTextField("");
        equationFieldTwo.setEditable(false);
        equationFieldTwo.setBounds(50, 180, 300,80);
        equationFieldTwo.setFont(new Font("楷体",Font.BOLD,32));
        equationFieldTwo.setHorizontalAlignment(JTextField.CENTER);
        equationFieldTwo.setBackground(new Color(255, 255, 255, 255));
        JTextField equationFieldThree = new JTextField("");
        equationFieldThree.setEditable(false);
        equationFieldThree.setBounds(50, 260, 300,80);
        equationFieldThree.setFont(new Font("楷体",Font.BOLD,32));
        equationFieldThree.setHorizontalAlignment(JTextField.CENTER);
        equationFieldThree.setBackground(new Color(255, 255, 255, 255));
        JTextField equationFieldFour = new JTextField("");
        equationFieldFour.setEditable(false);
        equationFieldFour.setBounds(50, 340, 300,80);
        equationFieldFour.setFont(new Font("楷体",Font.BOLD,32));
        equationFieldFour.setHorizontalAlignment(JTextField.CENTER);
        equationFieldFour.setBackground(new Color(255, 255, 255, 255));
        JTextField equationFieldFive = new JTextField("");
        equationFieldFive.setEditable(false);
        equationFieldFive.setBounds(50, 420, 300,80);
        equationFieldFive.setFont(new Font("楷体",Font.BOLD,32));
        equationFieldFive.setHorizontalAlignment(JTextField.CENTER);
        equationFieldFive.setBackground(new Color(255, 255, 255, 255));
        // 答案填写域
        JTextField answerFieldOne = new JTextField("");
        answerFieldOne.setEditable(true);
        answerFieldOne.setBounds(380, 100, 150,80);
        answerFieldOne.setFont(new Font("楷体",Font.BOLD,32));
        answerFieldOne.setHorizontalAlignment(JTextField.CENTER);
        answerFieldOne.setBackground(new Color(255, 255, 255, 255));
        JTextField answerFieldTwo = new JTextField("");
        answerFieldTwo.setEditable(true);
        answerFieldTwo.setBounds(380, 180, 150,80);
        answerFieldTwo.setFont(new Font("楷体",Font.BOLD,32));
        answerFieldTwo.setHorizontalAlignment(JTextField.CENTER);
        answerFieldTwo.setBackground(new Color(255, 255, 255, 255));
        JTextField answerFieldThree = new JTextField("");
        answerFieldThree.setEditable(true);
        answerFieldThree.setBounds(380, 260, 150,80);
        answerFieldThree.setFont(new Font("楷体",Font.BOLD,32));
        answerFieldThree.setHorizontalAlignment(JTextField.CENTER);
        answerFieldThree.setBackground(new Color(255, 255, 255, 255));
        JTextField answerFieldFour = new JTextField("");
        answerFieldFour.setEditable(true);
        answerFieldFour.setBounds(380, 340, 150,80);
        answerFieldFour.setFont(new Font("楷体",Font.BOLD,32));
        answerFieldFour.setHorizontalAlignment(JTextField.CENTER);
        answerFieldFour.setBackground(new Color(255, 255, 255, 255));
        JTextField answerFieldFive = new JTextField("");
        answerFieldFive.setEditable(true);
        answerFieldFive.setBounds(380, 420, 150,80);
        answerFieldFive.setFont(new Font("楷体",Font.BOLD,32));
        answerFieldFive.setHorizontalAlignment(JTextField.CENTER);
        answerFieldFive.setBackground(new Color(255, 255, 255, 255));
        // 结果展示域
        JTextField resultDisplayFieldOne = new JTextField();
        resultDisplayFieldOne.setEditable(false);
        resultDisplayFieldOne.setBounds(550, 100, 200,80);
        resultDisplayFieldOne.setFont(new Font("楷体",Font.BOLD,32));
        resultDisplayFieldOne.setHorizontalAlignment(JTextField.CENTER);
        resultDisplayFieldOne.setBackground(new Color(184,207,229));
        JTextField resultDisplayFieldTwo = new JTextField();
        resultDisplayFieldTwo.setEditable(false);
        resultDisplayFieldTwo.setBounds(550, 180, 200,80);
        resultDisplayFieldTwo.setFont(new Font("楷体",Font.BOLD,32));
        resultDisplayFieldTwo.setHorizontalAlignment(JTextField.CENTER);
        resultDisplayFieldTwo.setBackground(new Color(184,207,229));
        JTextField resultDisplayFieldThree = new JTextField();
        resultDisplayFieldThree.setEditable(false);
        resultDisplayFieldThree.setBounds(550, 260, 200,80);
        resultDisplayFieldThree.setFont(new Font("楷体",Font.BOLD,32));
        resultDisplayFieldThree.setHorizontalAlignment(JTextField.CENTER);
        resultDisplayFieldThree.setBackground(new Color(184,207,229));
        JTextField resultDisplayFieldFour = new JTextField();
        resultDisplayFieldFour.setEditable(false);
        resultDisplayFieldFour.setBounds(550, 340, 200,80);
        resultDisplayFieldFour.setFont(new Font("楷体",Font.BOLD,32));
        resultDisplayFieldFour.setHorizontalAlignment(JTextField.CENTER);
        resultDisplayFieldFour.setBackground(new Color(184,207,229));
        JTextField resultDisplayFieldFive = new JTextField();
        resultDisplayFieldFive.setEditable(false);
        resultDisplayFieldFive.setBounds(550, 420, 200,80);
        resultDisplayFieldFive.setFont(new Font("楷体",Font.BOLD,32));
        resultDisplayFieldFive.setHorizontalAlignment(JTextField.CENTER);
        resultDisplayFieldFive.setBackground(new Color(184,207,229));
        // 按钮
        JTextField scoresDisplayField = new JTextField("得分：0");
        scoresDisplayField.setEditable(false);
        scoresDisplayField.setBounds(800, 100, 350,80);
        scoresDisplayField.setFont(new Font("楷体",Font.BOLD,48));
        scoresDisplayField.setHorizontalAlignment(JTextField.CENTER);
        scoresDisplayField.setBackground(new Color(184,207,229));
        JButton submitButton = new JButton("提交答案");
        submitButton.setFont(new Font("楷体",Font.BOLD,32));
        submitButton.setBounds(800, 180,350, 70);
        submitButton.setFocusPainted(false);
        submitButton.setBorder(null);
        submitButton.setBackground(new Color(255, 255, 255, 255));
        JButton continueButton = new JButton("继续练习");
        continueButton.setFont(new Font("楷体",Font.BOLD,32));
        continueButton.setBounds(800, 260,350, 70);
        continueButton.setFocusPainted(false);
        continueButton.setBorder(null);
        continueButton.setBackground(new Color(255, 255, 255, 255));
        submitButton.addActionListener(e -> {
            int[] correctAnswer = {data.getData().get(index[0]).calculate(), data.getData().get(index[1]).calculate(),
                    data.getData().get(index[2]).calculate(), data.getData().get(index[3]).calculate(),
                    data.getData().get(index[4]).calculate()};
            if (answerFieldOne.getText().length() < 1) {
                resultDisplayFieldOne.setText("答案错误");
                wrongTime[0]++;
                wrongSetOfEquations.add(data.getData().get(index[0]));
            } else if (Integer.parseInt(answerFieldOne.getText()) == correctAnswer[0]) {
                resultDisplayFieldOne.setText("答案正确");
                score[0] += 5;
                correctTime[0]++;
                scoresDisplayField.setText("得分："+score[0]);
            } else {
                resultDisplayFieldOne.setText("答案错误");
                wrongTime[0]++;
                wrongSetOfEquations.add(data.getData().get(index[0]));
            }
            if (answerFieldTwo.getText().length() < 1) {
                resultDisplayFieldTwo.setText("答案错误");
                wrongTime[0]++;
                wrongSetOfEquations.add(data.getData().get(index[1]));
            }else if (Integer.parseInt(answerFieldTwo.getText()) == correctAnswer[1]) {
                resultDisplayFieldTwo.setText("答案正确");
                score[0] += 5;
                correctTime[0]++;
                scoresDisplayField.setText("得分："+score[0]);
            } else {
                resultDisplayFieldTwo.setText("答案错误");
                wrongTime[0]++;
                wrongSetOfEquations.add(data.getData().get(index[1]));
            }
            if (answerFieldThree.getText().length() < 1) {
                resultDisplayFieldThree.setText("答案错误");
                wrongTime[0]++;
                wrongSetOfEquations.add(data.getData().get(index[2]));
            } else if (Integer.parseInt(answerFieldThree.getText()) == correctAnswer[2]) {
                resultDisplayFieldThree.setText("答案正确");
                score[0] += 5;
                correctTime[0]++;
                scoresDisplayField.setText("得分："+score[0]);
            } else {
                resultDisplayFieldThree.setText("答案错误");
                wrongTime[0]++;
                wrongSetOfEquations.add(data.getData().get(index[2]));
            }
            if (answerFieldFour.getText().length() < 1) {
                resultDisplayFieldFour.setText("答案错误");
                wrongTime[0]++;
                wrongSetOfEquations.add(data.getData().get(index[3]));
            } else if (Integer.parseInt(answerFieldFour.getText()) == correctAnswer[3]) {
                resultDisplayFieldFour.setText("答案正确");
                score[0] += 5;
                correctTime[0]++;
                scoresDisplayField.setText("得分："+score[0]);
            } else {
                resultDisplayFieldFour.setText("答案错误");
                wrongTime[0]++;
                wrongSetOfEquations.add(data.getData().get(index[3]));
            }
            if (answerFieldFive.getText().length() < 1) {
                resultDisplayFieldFive.setText("答案错误");
                wrongTime[0]++;
                wrongSetOfEquations.add(data.getData().get(index[4]));
            } else if (Integer.parseInt(answerFieldFive.getText()) == correctAnswer[4]) {
                resultDisplayFieldFive.setText("答案正确");
                score[0] += 5;
                correctTime[0]++;
                scoresDisplayField.setText("得分："+ score[0]);
            } else {
                resultDisplayFieldFive.setText("答案错误");
                wrongTime[0]++;
                wrongSetOfEquations.add(data.getData().get(index[4]));
            }
            submitButton.setEnabled(false);
            continueButton.setEnabled(true);
        });
        continueButton.addActionListener(e -> {
            Random random = new Random();
            for (int i = 0; i < 5; i++) {
                index[i] = random.nextInt(data.getData().size() + 1);
            }
            equationFieldOne.setText(data.getData().get(index[0]).toString());
            equationFieldTwo.setText(data.getData().get(index[1]).toString());
            equationFieldThree.setText(data.getData().get(index[2]).toString());
            equationFieldFour.setText(data.getData().get(index[3]).toString());
            equationFieldFive.setText(data.getData().get(index[4]).toString());
            resultDisplayFieldOne.setText("");
            resultDisplayFieldTwo.setText("");
            resultDisplayFieldThree.setText("");
            resultDisplayFieldFour.setText("");
            resultDisplayFieldFive.setText("");
            answerFieldOne.setText("");
            answerFieldTwo.setText("");
            answerFieldThree.setText("");
            answerFieldFour.setText("");
            answerFieldFive.setText("");
            submitButton.setEnabled(true);
            continueButton.setEnabled(false);
        });
        JButton backButton = new JButton("返回菜单");
        backButton.setFont(new Font("楷体",Font.BOLD,32));
        backButton.setBounds(800, 425,350, 75);
        backButton.setFocusPainted(false);
        backButton.setBorder(null);
        backButton.setBackground(new Color(255, 255, 255, 255));
        backButton.addActionListener(e -> cardLayout.show(cards, "mainPanel"));
        JButton showStatisticsButton = new JButton("查看统计");
        showStatisticsButton.setFont(new Font("楷体",Font.BOLD,32));
        showStatisticsButton.setBounds(800, 340,350, 75);
        showStatisticsButton.setFocusPainted(false);
        showStatisticsButton.setBorder(null);
        showStatisticsButton.setBackground(new Color(255, 255, 255, 255));
        showStatisticsButton.addActionListener(e -> {
            cardLayout.show(cards, "panelThree");
            correctTimeField.setText("答对: " + correctTime[0] + "道题");
            wrongTimeField.setText("答错: " + wrongTime[0] + "道题");
            if (correctTime[0] + wrongTime[0] != 0) {
                correctRate[0] = correctTime[0] * 1.0 / (correctTime[0] + wrongTime[0]);
            }
            correctRateField.setText("正确率: " + num.format(correctRate[0]));
        });
        panelOne.add(backButton);
        panelOne.add(submitButton);
        panelOne.add(continueButton);
        panelOne.add(showStatisticsButton);
        panelOne.add(scoresDisplayField);
        panelOne.add(equationFieldOne);
        panelOne.add(answerFieldOne);
        panelOne.add(resultDisplayFieldOne);
        panelOne.add(equationFieldTwo);
        panelOne.add(answerFieldTwo);
        panelOne.add(resultDisplayFieldTwo);
        panelOne.add(equationFieldThree);
        panelOne.add(answerFieldThree);
        panelOne.add(resultDisplayFieldThree);
        panelOne.add(equationFieldFour);
        panelOne.add(answerFieldFour);
        panelOne.add(resultDisplayFieldFour);
        panelOne.add(equationFieldFive);
        panelOne.add(answerFieldFive);
        panelOne.add(resultDisplayFieldFive);

        // 主界面：
        JTextField welcomeField = new JTextField("欢迎");
        welcomeField.setBackground(new Color(184,207,229));
        welcomeField.setFont(new Font("楷体",Font.BOLD,100));
        welcomeField.setBounds(0, 20, 1200, 100);
        welcomeField.setHorizontalAlignment(JTextField.CENTER);
        welcomeField.setEditable(false);
        welcomeField.setBorder(null);
        mainPanel.add(welcomeField);
        JButton startButton = new JButton("开始");
        startButton.setFont(new Font("楷体",Font.BOLD,64));
        startButton.setBounds(30, 150,550, 352);
        startButton.setFocusPainted(false);
        startButton.setBorder(null);
        startButton.setBackground(new Color(255, 255, 255, 255));
        startButton.addActionListener(e -> {
            cardLayout.show(cards, "panelOne");
            Random random = new Random();
            for (int i = 0; i < 5; i++) {
                index[i] = random.nextInt(data.getData().size() + 1);
            }
            continueButton.setEnabled(false);
            submitButton.setEnabled(true);
            equationFieldOne.setText(data.getData().get(index[0]).toString());
            equationFieldTwo.setText(data.getData().get(index[1]).toString());
            equationFieldThree.setText(data.getData().get(index[2]).toString());
            equationFieldFour.setText(data.getData().get(index[3]).toString());
            equationFieldFive.setText(data.getData().get(index[4]).toString());
            answerFieldFour.setText("");
            answerFieldFour.setText("");
            answerFieldFour.setText("");
            answerFieldFour.setText("");
            answerFieldFour.setText("");
            resultDisplayFieldOne.setText("");
            resultDisplayFieldTwo.setText("");
            resultDisplayFieldThree.setText("");
            resultDisplayFieldFour.setText("");
            resultDisplayFieldFive.setText("");
        });
        mainPanel.add(startButton);
        JButton wrongEquationSetButton = new JButton("错题簿");
        wrongEquationSetButton.setFont(new Font("楷体",Font.BOLD,64));
        wrongEquationSetButton.setBounds(600, 150,550, 171);
        wrongEquationSetButton.setFocusPainted(false);
        wrongEquationSetButton.setBorder(null);
        wrongEquationSetButton.setBackground(new Color(255, 255, 255, 255));
        wrongEquationSetButton.addActionListener(e -> {
            cardLayout.show(cards, "panelTwo");
            titleField.setText("您目前共有" + wrongSetOfEquations.getData().size() + "道错题");
            Iterator iterator = wrongSetOfEquations.getIterator();
            wrongEquationDisplayArea.setLineWrap(true);
            int cnt = 0;
            while (iterator.existNext()) {
                wrongEquationDisplayArea.append("("+ ++cnt + ")" + iterator.next() + ' ');
                if (cnt % 3 == 0) {
                    wrongEquationDisplayArea.append("\n");
                }
            }
        });
        mainPanel.add(wrongEquationSetButton);
        JButton statistics = new JButton("统计");
        statistics.setFont(new Font("楷体",Font.BOLD,64));
        statistics.setBounds(600, 331,270, 171);
        statistics.setFocusPainted(false);
        statistics.setBorder(null);
        statistics.setBackground(new Color(255, 255, 255, 255));
        statistics.addActionListener(e -> {
            cardLayout.show(cards, "panelThree");
            correctTimeField.setText("答对: " + correctTime[0] + "道题");
            wrongTimeField.setText("答错: " + wrongTime[0] + "道题");
            if (correctTime[0] + wrongTime[0] != 0) {
                correctRate[0] = correctTime[0] * 1.0 / (correctTime[0] + wrongTime[0]);
            }
            correctRateField.setText("正确率: " + num.format(correctRate[0]));
        });
        mainPanel.add(statistics);
        JButton exitButton = new JButton("退出");
        exitButton.setFont(new Font("楷体",Font.BOLD,64));
        exitButton.setBounds(880, 331,270, 171);
        exitButton.setFocusPainted(false);
        exitButton.setBorder(null);
        exitButton.setBackground(new Color(255, 255, 255, 255));
        exitButton.addActionListener(e -> System.exit(0));
        mainPanel.add(exitButton);

        cards.add(mainPanel, "mainPanel");
        cards.add(panelOne, "panelOne");
        cards.add(panelTwo, "panelTwo");
        cards.add(panelThree, "panelThree");
        jFrame.add(cards);
        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 获得窗口宽
        int windowWidth = jFrame.getWidth();
        // 获得窗口高
        int windowHeight = jFrame.getHeight();
        // 定义工具包
        Toolkit kit = Toolkit.getDefaultToolkit();
        // 获取屏幕的尺寸
        Dimension screenSize = kit.getScreenSize();
        // 获取屏幕的宽
        int screenWidth = screenSize.width;
        // 获取屏幕的高
        int screenHeight = screenSize.height;
        // 设置窗口居中显示
        jFrame.setLocation(screenWidth / 2 - windowWidth / 2, screenHeight / 2 - windowHeight / 2);
        // 显示主面板
        cardLayout.show(cards, "mainPanel");
    }
}

